import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

import javax.swing.JLabel;


@SuppressWarnings("serial")
public class ImagePanel extends JLabel {

	BufferedImage image;
	Image scaled_image;
	Dimension image_size,scaled_size;
	int scale_pos;
	double scale[] = new double[]{0.1, 0.2, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0};

	public ImagePanel() {
		scale_pos=3;
		image_size=new Dimension(800,600);
		scaled_size=new Dimension((int)(image_size.width*scale[scale_pos]),(int)(image_size.height*scale[scale_pos]));
		setBackground(Color.white);

		image=null;
		scaled_image=null;
	}
	
	public BufferedImage getImage()
	{
		WritableRaster raster=image.copyData(null);
		return new BufferedImage(image.getColorModel(),raster,image.isAlphaPremultiplied(),null);
	}

	public void setImage(BufferedImage image)
	{
		this.image=image;
		image_size.width=image.getWidth();
		image_size.height=image.getHeight();
		scaled_size.width=(int)(image_size.width*scale[scale_pos]);
		scaled_size.height=(int)(image_size.height*scale[scale_pos]);
		scaled_image=image.getScaledInstance(scaled_size.width, scaled_size.height, BufferedImage.SCALE_FAST);
	}

	public Dimension getPreferredSize()
	{		
		return scaled_size;
	}

	public void paint(Graphics g) {

		if(scaled_image==null) return;		
		g.drawImage(scaled_image,0,0,null);		
		super.paint(g);
	}

	public void zoomIn()
	{
		if(scale_pos<scale.length-1) 
		{
			scale_pos++;			
			if(image==null) return;			
			scaled_size.width=(int)(image_size.width*scale[scale_pos]);
			scaled_size.height=(int)(image_size.height*scale[scale_pos]);
			scaled_image=image.getScaledInstance(scaled_size.width, scaled_size.height, BufferedImage.SCALE_FAST);
			revalidate();
			repaint();
		}
	}

	public void zoomOut()
	{
		if(scale_pos>0) 
		{
			scale_pos--;			
			if(image==null) return;			
			scaled_size.width=(int)(image_size.width*scale[scale_pos]);
			scaled_size.height=(int)(image_size.height*scale[scale_pos]);
			scaled_image=image.getScaledInstance(scaled_size.width, scaled_size.height, BufferedImage.SCALE_FAST);
			revalidate();
			repaint();

		}
	}

}
